<?php

class Admin_NewsletterController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
       $this->_helper->layout()->disableLayout();
       Zend_Loader::loadFile('../application/admin/models/Newsletter.php');
       $newsletter_model = new Newsletter();
       $newsletter_arr = $newsletter_model->listNewsletters();       
       $this->view->newsletter_arr = $newsletter_arr;
     
    }
    
    public function addAction()
    {
        $this->_helper->layout()->disableLayout();
		Zend_Loader::loadFile('../application/admin/models/Template.php');		
        $mod_template = new Template();     
        $tpl_arr = $mod_template->getTemplateInfo();
        $this->view->title = "Add Newsletter";        
        $this->view->tpl_arr = $tpl_arr;      
    }
    
    public function doaddnewsletterAction()
	{
		Zend_Loader::loadFile('../application/admin/forms/Newsletter.php');
		Zend_Loader::loadFile('../application/admin/models/Newsletter.php');
		$form_newsletter = new Form_Newsletter();
		$request = $this->getRequest();
		
		if ($this->getRequest()->isPost()) {            
			$newsletter_model = new Newsletter();
			$form_values = $request->getPost();
			$add_newsletter = $newsletter_model->addNewsletter($form_values);
		}
		
		exit;
	}

    public function editAction()
    {
    	$this->_helper->layout()->disableLayout();
        Zend_Loader::loadFile('../application/admin/forms/Newsletter.php');
        Zend_Loader::loadFile('../application/admin/models/Newsletter.php');
        Zend_Loader::loadFile('../application/admin/models/Template.php');		
        
        $newsletter_model = new Newsletter();
        $mod_template = new Template();     
        
        $controller = $this->getRequest()->getControllerName();
		$action = $this->getRequest()->getActionName();
		$newsletter_id = $this->_getParam('newsletter_id', 0);
		$options = array("action"=>$action, "newsletter_id"=>$newsletter_id);
        $newsletter_form = new Form_Newsletter($options);
        $tpl_arr = $mod_template->getTemplateInfo();
        
        $this->view->newsletter_form = $newsletter_form;        
        $this->view->newsletter_id = $newsletter_id;
        $newsletter_arr = $newsletter_model->getNewsletter($newsletter_id); 
        $this->view->newsletter_arr = $newsletter_arr;
        $this->view->tpl_arr = $tpl_arr;      
    }

    public function updatenewsletterAction()
    {
        $this->_helper->layout()->disableLayout();
   		Zend_Loader::loadFile('../application/admin/models/Newsletter.php');
		Zend_Loader::loadFile('../application/admin/forms/Newsletter.php');
        $newsletter_form = new Form_Newsletter();
        $request = $this->getRequest();
		if ($this->getRequest()->isPost()) {            
			$newsletter_model = new Newsletter();
			$form_values = $request->getPost();
			$update_newsletter = $newsletter_model->updateNewsletter($form_values);
		}
		
		exit;
    }
    
    public function deleteAction()
    {
        $this->_helper->layout()->disableLayout();
        $newsletter_id = $this->_getParam('n_id', 0);    
        Zend_Loader::loadFile('../application/admin/models/Newsletter.php');
        $Newsletter_model = new Newsletter();
        $Newsletter_model->deleteNewsletter($newsletter_id);
        //$this->_helper->redirector('index');
        exit;
    }
    
    public function listsubuserAction()
    {
    	$this->_helper->layout()->disableLayout();
    	Zend_Loader::loadFile('../application/admin/models/Newsletter.php');
    	
    	$mod_newsletter = new Newsletter();
    	$mail = new Zend_Mail();
    	$request = $this->getRequest();
    	$newsletter_id = $request->getParam('newsletter_id');
    	$subs_user_arr_en = $mod_newsletter->getSubscribeUsersByLang('en');
    	$subs_user_arr_ru = $mod_newsletter->getSubscribeUsersByLang('ru');
    	$newsletter_arr = $mod_newsletter->getNewsletter($newsletter_id);
    	
    	$this->view->newsletter_id = $newsletter_id;
    	//$this->view->newsletter_content_en = $newsletter_arr['newsletter_content_en'];
    	//$this->view->newsletter_content_ru = $newsletter_arr['newsletter_content_ru'];
    	
    	$this->view->subs_user_arr_en = $subs_user_arr_en;
    	$this->view->subs_user_arr_ru = $subs_user_arr_ru;
    	
    }
    
    public function sendnewsletterAction()
    {
    	$this->_helper->layout()->disableLayout();
    	Zend_Loader::loadFile('../application/admin/models/Newsletter.php');
    	$mod_newsletter = new Newsletter();
        $recipient="";
    	
    	$request = $this->getRequest();
    	$newsletter_id = $request->getParam('newsletter_id');
    	$lang = $request->getParam('lang');
    	$email = $request->getParam('email');
    	
    	$mail = new Zend_Mail();
    	//$mail->setHeaderEncoding(Zend_Mime::ENCODING_BASE64);
    	//$mail->CharSet = 'UTF-8';
    	$newsletter_arr = $mod_newsletter->getNewsletter($newsletter_id);
    	
    	if($lang == 'en') {
    		$newsletter_content_en = $newsletter_arr['newsletter_content_en'];
    		$msg = $newsletter_content_en;
    		$subject = "Warnertravellers Newsletter";
    	} else if($lang == 'ru') {
    		$newsletter_content_ru = $newsletter_arr['newsletter_content_ru'];
    		$msg = $newsletter_content_ru;
    		$subject = "Warner ??? ????";
    	}
    	
    	
	//$headers = "Content-type: text/html; charset=UTF-8\r\n";
        $headers = "From: Warnertravellers.com <info@warnertravellers.com>\r\n";
        $mail->setBodyHtml($msg);
        $mail ->setFrom($headers);
        $mail->setSubject($subject);

        foreach ($email as $val) {                    
            $mail->clearRecipients();   //clears the recipient list otherwise the same newletter will be sent more than once to the same user
            $mail->addTo($val);                    
            if($mail->send()) {
                $recipient.="\r\n".$val. " - success";
                //echo "Newsletter has been successfully send";
            }else{
                $recipient="\r\n".$val." - failed";
            }                    
        }
                //exit($recipient);
            /*$mail->setBodyHtml($msg);
	    $mail ->setFrom($headers);
	    $mail->setSubject($subject);*/
	    
		// for the testing purpose we are sending email to the localhost.
	    //$mail->addTo('info@localhost'); 
	    
	    
	    if(!empty($recipient)) {                
	    	//echo "Newsletter has been successfully sent to the following recipients;".$recipient;
                echo "Newsletter sending status;".$recipient;
	    }
    	
    	exit;
    }
}